package helper;

import jodd.io.FileNameUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.libs.WS;
import play.mvc.Http;

/**
 * URL相关工具类.
 */
public class UrlHelper {
    private static final Logger logger = LoggerFactory.getLogger(UrlHelper.class);

    /**
     * 从地址中得到subdomain
     * @param host
     * @return
     */
    public static String getSubdomain(String host) {
        if (StringUtils.isBlank(host)) {
            return null;
        }
        String[] names = host.split("[\\.:]");
        return names[0];
    }

    public static String addParam(String key, String value) {
        String url = Http.Request.current().url;
        if (url.contains(key + "=")) {
            url = url.replaceAll(key + "=[^&]+", key + "=" + WS.encode(value));
        } else {
            url += (url.contains("?") ? '&' : '?') + key + "=" + WS.encode(value);
        }

        return url;
    }

    /**
     * 得到OSS上传图片的URL.
     */
    public static String getUploadOssImageUrl(String objectName) {
        return getUploadOssImageUrl(objectName, null);
    }

    /**
     * 得到OSS上传图片的URL, 带处理动作.
     * <p>
     * 可用action见: https://docs.aliyun.com/#/pub/oss/oss-img-api/image-processing&resize
     */
    public static String getUploadOssImageUrl(String objectName, String action) {
        String imgSrc;
        if (objectName.startsWith("http")) {
            imgSrc = objectName; //直接返回
        } else {
            imgSrc = GlobalConfig.IMG_SERVER_BASE_URL + objectName;
        }

        if (action != null) {
            String fileExt = "." + FileNameUtil.getExtension(objectName);
            fileExt = fileExt.toLowerCase();
            if (action.endsWith(fileExt)) {
                imgSrc += "@" + action;
            } else {
                imgSrc += "@" + action + fileExt;
            }

        }
        return imgSrc;
    }

    /**
     * PC网站版本的baseUrl.
     */
    public static String getWebsiteBaseUrl() {
        // 可能http,也可能https
        if (GlobalConfig.APPLICATION_URL_SSL) {
            return "https://" + GlobalConfig.APPLICATION_BASE_DOMAIN;
        }
        return "http://" + GlobalConfig.APPLICATION_BASE_DOMAIN;
    }

    public static String getDefaultHeadUrl() {
        // 可能http,也可能https
        if (GlobalConfig.APPLICATION_URL_SSL) {
            return "https://a.cdn6.cn/assets/default-head.jpg";
        }
        return "http://a.cdn6.cn/assets/default-head.jpg";
    }

    public static String getHttpPrefix() {
        if (GlobalConfig.APPLICATION_URL_SSL) {
            return "https://";
        }
        return "http://";
    }

    public static String getWebsiteBaseUrl(String path) {
        return getWebsiteBaseUrl() + getContextPath(path);
    }


    public static String getContextPath(String path) {
        return getDefaultContextPath() + path;
    }

    /**
     * 非 / 结尾.
     */
    public static String getDefaultContextPath() {
        if (GlobalConfig.CONTEXT_PATH.endsWith("/")) {
            return GlobalConfig.CONTEXT_PATH.substring(0, GlobalConfig.CONTEXT_PATH.length() - 1);
        }
        return GlobalConfig.CONTEXT_PATH;
    }

    /**
     * Api接口URL
     * @return  like http://api.hyshi.net/api/v2.0
     */
    public static String getApiBaseUrl() {
        return getWebsiteBaseUrl("/api/v2.0");
    }

}
